const rawModules = import.meta.glob("./components/*.js", { as: "raw" });
const modules = import.meta.glob("./components/*.js");

import { getFieldConfig } from "./fieldConfig";

const fieldConfig = getFieldConfig('Element')

export { getFieldConfig };

export const fieldMenuList = Object.keys(fieldConfig)
  .filter((item) => item !== "subForm")
  .reduce((list, fieldType) => {
    const { title } = fieldConfig[fieldType];
    list.push({
      type: fieldType,
      title,
    });
    return list;
  }, []);

const compMap = {};
let init = false;

export async function resolveRuntimeComp() {
  if (init) return compMap;
  for (const path in modules) {
    const fileName = /\.\/components\/([\w]+)\.js/.exec(path)?.[1] ?? "";
    const content = await rawModules[path]();
    const comp = await modules[path]();
    compMap[fileName] = {
      compStr: content,
      comp,
    };
  }
  init = true;
  return compMap;
}
